package com.glorysoft.hlgdpda.ui.trackin.viewmodel

import androidx.databinding.ObservableArrayList
import androidx.lifecycle.MutableLiveData
import com.glorysoft.hlgdpda.R
import com.glorysoft.hlgdpda.base.BaseViewModel
import com.glorysoft.hlgdpda.entity.CarrierEntity
import com.glorysoft.hlgdpda.entity.LotEntity
import com.glorysoft.hlgdpda.entity.MLotListEntity
import com.glorysoft.hlgdpda.entity.ui.CallObserveBean
import com.glorysoft.hlgdpda.entity.ui.ToastMessageBean
import com.glorysoft.hlgdpda.model.TaskRepository
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.glorysoft.hlgdpda.BR
import me.tatarka.bindingcollectionadapter2.ItemBinding

/**
 * FileName: NormalTrackInViewModel
 * Author: XJH
 * Date: 2021/7/19 11:15
 * Description:
 * History:
 */
class NormalTrackInViewModel : BaseViewModel() {

    val carrierList: List<String> = listOf("批号", "产品名称", "数量", "工步名称", "载具ID", "状态","工单号")
    val materialList: List<String> = listOf("No.", "物料批号", "物料描述", "数量", "特征码", "操作")
    var lotEntity = MutableLiveData<LotEntity>()
    private val trackInRepository = TaskRepository()

    private val itemMaterialOnClickListener = object : TrackInViewModel.OnItemClickListener {
        override fun onItemClick(item: Any) {
            defUI.callObserve.value = CallObserveBean(1, item)
        }
    }

//    private val itemUpCarrierOnClickListener = object : TrackInViewModel.OnItemClickListener {
//        override fun onItemClick(item: Any) {
//            defUI.callObserve.value = CallObserveBean(2, item)
//        }
//    }
//
//    private val itemDownCarrierOnClickListener = object : TrackInViewModel.OnItemClickListener {
//        override fun onItemClick(item: Any) {
//            defUI.callObserve.value = CallObserveBean(3, item)
//        }
//    }

    //物料列表
    var mLotItems = ObservableArrayList<MLotListEntity.MLotEntity.MLotInfo>()
    var mMaterielBinding =
        ItemBinding.of<MLotListEntity.MLotEntity.MLotInfo>(BR.item, R.layout.item_mlot)
            .bindExtra(BR.deleteListener, itemMaterialOnClickListener)

    //载具列表
    var carrierUpItems = ObservableArrayList<LotEntity>()
    var carrierUpItemsBinding =
        ItemBinding.of<LotEntity>(BR.carrierItem, R.layout.item_carrier)

    //载具列表
    var carrierDownItems = ObservableArrayList<LotEntity>()
    var carrierDownItemsBinding =
        ItemBinding.of<LotEntity>(BR.carrierItem, R.layout.item_carrier)

    /**
     * 获取批次信息
     */
    fun searchLotInfo(lot: String, eqp: String) {
        val map = JsonObject()
        map.addProperty("ACTIONTYPE", "LOTINFOBYID")
        map.addProperty("LOTID", lot)
        map.addProperty("EQUIPMENTID", eqp)
        launchOnlyResult({
            val lotInfo = trackInRepository.getLotInfo(map)
            lotEntity.value = lotInfo
            defUI.callObserve.value = CallObserveBean(200, lotInfo)
        })
    }

    fun searchCarrierInfo(carrier: String, type: Int) {
        val map = JsonObject()
        map.addProperty("CARRIERID", carrier)
        launchOnlyResult({
            val lotInfo = trackInRepository.getLotInfoByCarrier(map)
            if (type == 1) {
                carrierUpItems.clear()
            } else {
                carrierDownItems.clear()
            }

            lotEntity.value = lotInfo
            carrierUpItems.add(lotInfo)
            defUI.callObserve.value = CallObserveBean(200, lotInfo)
//            getCarrier(carrier, type)

        })
    }

    private fun getCarrier(carrier: String, type: Int) {
        val map = JsonObject()
        map.addProperty("CARRIERID", carrier)
        launchOnlyResult({
            val carrierInfo = trackInRepository.getCarrierInfo(map)
            if (type == 1) {
                carrierInfo.CARRIER.position = (carrierUpItems.size + 1).toString()
//                carrierUpItems.add(carrierInfo.CARRIER)
            } else {
                carrierInfo.CARRIER.position = (carrierDownItems.size + 1).toString()
//                carrierDownItems.add(carrierInfo.CARRIER)
            }
        })
    }


    fun defaultTrackIn(eqpID: String) {
        if (lotEntity.value == null) return
        val map = JsonObject()
        map.addProperty("ACTIONTYPE", if (eqpID.isEmpty()) "NoEqpTrackIn" else "DefaultTrackIn")
        //todo://   是否检查工步权限
        map.addProperty("ISCHECKSTEPAUTHORITY", true)
        map.addProperty("EQUIPMENTID", eqpID)
//        map.addProperty("CARRIERID", "")
        var list = mutableListOf<LotEntity.LotInfoEntity?>();
        list.add(lotEntity.value?.LOT)
        map.add("LOTLIST", JsonParser().parse(Gson().toJson(list)).asJsonArray)
        map.add("MATERIALLIST", JsonParser().parse(Gson().toJson(mLotItems)).asJsonArray)
        launchOnlyResult({
            trackInRepository.defaultTrackIn(map)
            defUI.toastEvent.value = ToastMessageBean("进站成功", true)
            lotEntity.value = null
            mLotItems.clear()
            carrierUpItems.clear()
            defUI.startActivity.call()
        })
    }

    /**
     * 获取物料信息
     */
    fun searchMaterial(eqp: String) {
        val map = JsonObject()
        map.addProperty("ACTIONTYPE", "LOAD")
        map.addProperty("EQUIPMENTID", eqp)
        launchOnlyResult({
            mLotItems.clear()
            val mLotInfo = trackInRepository.getMLotInfo(map)
            mLotInfo.MLOTLIST.forEach {
                if (it.MLOT.MLOTID.isNotEmpty()) {
                    it.MLOT.position = (mLotInfo.MLOTLIST.indexOf(it) + 1).toString()
                    mLotItems.add(it.MLOT)
                }
            }


        })
    }

    /**
     * 列表删除信息
     */
    fun deleteInfo(type: String, item: Any) {
        when (type) {
            "material" -> mLotItems.remove(item as MLotListEntity.MLotEntity)
            "carrierUp" -> carrierUpItems.remove(item as CarrierEntity.CarrierInfoEntity)
            "carrierDown" -> carrierDownItems.remove(item as CarrierEntity.CarrierInfoEntity)
        }
    }


}